unit NoticeM;

interface

uses
  Windows, Classes, SysUtils{$IFDEF AUTO_ENCODING}, uCODE_Convert{$ENDIF};

type
  TNoticeMsg = record
    sMsg: string;
    sList: TStringList;
  end;
  TNoticeList = array[0..99] of TNoticeMsg;

  TNoticeManager = class
  private
    NoticeList: TNoticeList;
  public
    constructor Create();
    destructor Destroy; override;
    function GetNoticeMsg(sStr: string; LoadList: TStringList): Boolean;
    procedure LoadingNotice();
    {$IFDEF JS}
    property _NoticeList: TNoticeList read NoticeList write NoticeList;
    {$ENDIF}
  end;
  
implementation

uses M2Share;

constructor TNoticeManager.Create;
var
  i                         : Integer;
begin
  for i := Low(NoticeList) to High(NoticeList) do begin
    NoticeList[i].sMsg := '';
    NoticeList[i].sList := nil;
  end;
end;

destructor TNoticeManager.Destroy;
var
  i                         : Integer;
begin
  for i := Low(NoticeList) to High(NoticeList) do begin
    if NoticeList[i].sList <> nil then
      NoticeList[i].sList.Free;
  end;
  inherited;
end;

procedure TNoticeManager.LoadingNotice();
var
  sFileName                 : string;
  i                         : Integer;
  {$IFDEF LOADER}
  stm: TMemoryStream;
  {$ENDIF}
begin
  for i := Low(NoticeList) to High(NoticeList) do begin
    if NoticeList[i].sMsg = '' then
      Continue;
    {$IFNDEF LOADER}
    sFileName := g_Config.sNoticeDir + NoticeList[i].sMsg + '.txt';
    if FileExists(sFileName) then begin
    {$ELSE}
      sFileName := 'Notice\' + NoticeList[i].sMsg + '.txt';
      stm := g_ServerPackage.GetFileUnZippedStreamIfExists(sFileName);
      if stm = nil then
      begin
        continue;
      end;
    {$ENDIF}
      try
        if NoticeList[i].sList = nil then
          NoticeList[i].sList := TStringList.Create;
        {$IFNDEF LOADER}

        try
          NoticeList[i].sList.LoadFromFile(sFileName{$IFDEF AUTO_ENCODING}, GetFileEncoding(sFileName){$ENDIF});
        except
          try
            NoticeList[i].sList.LoadFromFile(sFileName);
          except
            MainOutMessageAPI('loading fail.... => ' + sFileName);
          end;
        end;

        {$ELSE}
        NoticeList[i].sList.LoadFromStream(stm);
        stm.Free;
        {$ENDIF}
      except
        MainOutMessageAPI('Error in loading notice text. file name is ' + sFileName);
      end;
    {$IFNDEF LOADER}
    end;
    {$ENDIF}
  end;
end;

function TNoticeManager.GetNoticeMsg(sStr: string; LoadList: TStringList): Boolean;
var
  bo15                      : Boolean;
  n14                       : Integer;
  sFileName                 : string;
  {$IFDEF LOADER}
  stm: TMemoryStream;
  {$ENDIF}
begin
  Result := False;
  bo15 := True;
  for n14 := Low(NoticeList) to High(NoticeList) do begin
    if CompareText(NoticeList[n14].sMsg, sStr) = 0 then begin
      if NoticeList[n14].sList <> nil then begin
        LoadList.AddStrings(NoticeList[n14].sList);
        Result := True;
      end;
      bo15 := False;
    end;
  end;
  if not bo15 then
    Exit;
  for n14 := Low(NoticeList) to High(NoticeList) do begin
    if NoticeList[n14].sMsg = '' then begin
      {$IFNDEF LOADER}
      sFileName := g_Config.sNoticeDir + sStr + '.txt';
      if FileExists(sFileName) then begin
      {$ELSE}
        sFileName := 'Notice\' + sStr + '.txt';
        stm := g_ServerPackage.GetFileUnZippedStreamIfExists(sFileName);
        if stm = nil then
          continue;
      {$ENDIF}
        try
          if NoticeList[n14].sList = nil then
            NoticeList[n14].sList := TStringList.Create;
          {$IFNDEF LOADER}
          NoticeList[n14].sList.LoadFromFile(sFileName{$IFDEF AUTO_ENCODING}, GetFileEncoding(sFileName){$ENDIF});
          try
            NoticeList[n14].sList.LoadFromFile(sFileName{$IFDEF AUTO_ENCODING}, GetFileEncoding(sFileName){$ENDIF});
          except
            try
              NoticeList[n14].sList.LoadFromFile(sFileName);
            except
              MainOutMessageAPI('loading fail.... => ' + sFileName);
            end;
          end;
          {$ELSE}

          if stm <> nil then
          begin
            NoticeList[n14].sList.LoadFromStream(stm);
            stm.Free;
          end;

          {$ENDIF}
          LoadList.AddStrings(NoticeList[n14].sList);
        except
          MainOutMessageAPI('Error in loading notice text. file name is ' + sFileName);
        end;
        NoticeList[n14].sMsg := sStr;
        Result := True;
        Break;
      {$IFNDEF LOADER}
      end;
      {$ENDIF}
    end;
  end;
end;

end.

